package com.itheima.util;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

//JWT的生成和解析
public class JwtUtil {
    // 创建token
    public static String createToken(Map claims) {
        return Jwts.builder()
                .setClaims(claims) //设置响应数据体
                .signWith(SignatureAlgorithm.HS256, "heima_movies") //设置加密方法和加密盐
                .compact();
    }

    // 解析token
    public static Map parseToken(String token) {
        return Jwts.parser().setSigningKey("heima_movies")
                .parseClaimsJws(token)
                .getBody();
    }



    @Test
    public void test01()throws Exception{
        // 制作token
        Map<String,Object> claims = new HashMap<>();
        claims.put("usernaem", "jack");

        String token = createToken(claims);
        System.out.println(token);
    }

    @Test
    public void test02()throws Exception{
        Map map = parseToken("eyJhbGciOiJIUzI1NiJ9.eyJwYXNzd2qwwI6IjEyMyIsInVzZXJuYWVtIjoiamFjayJ9.-Gzi48LeGbb5cnmpqzu_37jddKTULI0pwID_r2Htmgo");
        System.out.println(map);
    }

}
